package com.wtgroup.demo.牛客;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @author nisus
 * @version 0.1
 * @since 2023/6/6 15:11
 */
public class HJ88_扑克牌大小 {

    public static void main(String[] args) {

    }


    // 通过
    // 太繁琐....
    public static class D {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            while (in.hasNextLine()) { // 注意 while 处理多个 case
                String line = in.nextLine();
                String[] split = line.split("-");
                Pai pai1 = new Pai(split[0]);
                Pai pai2 = new Pai(split[1]);

                Pai big = pai1.compare(pai2);

                if (big == null) {
                    System.out.println("ERROR");
                } else {
                    System.out.println(big.join());
                }


            }
        }

        static class Pai {
            static HashMap<String, Integer> map = new HashMap<>();

            static {
                // 3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER
                for (int i = 3; i <= 10; i++) {
                    map.put(i + "", i);
                }
                map.put("J", 11);
                map.put("Q", 12);
                map.put("K", 13);
                map.put("A", 14);
                map.put("2", 15);
                map.put("joker", 16);
                map.put("JOKER", 17);
            }

            String[] eles;
            int type; // 1 对大王, 2 对小王, 3 大小王, 4 4个炸弹, 5 3个, 6 对子, 7 个子, 8 顺子(5张)

            public Pai(String pai) {
                String[] split = pai.split(" ");
                this.eles = split;
                if (split.length == 1) {
                    this.type = 7;
                } else if (split.length == 2) {
                    if (split[0].equals("JOKER") && split[1].equals("JOKER")) {
                        this.type = 1;
                    } else if (split[0].equals("joker") && split[1].equals("JOKER")) {
                        this.type = 3;
                    } else if (split[0].equals("joker") && split[1].equals("joker")) {
                        this.type = 2;
                    } else if (split[0].equals(split[1])) {
                        this.type = 6;
                    } else {
                        throw new RuntimeException("input error");
                    }
                } else if (split.length == 3) { //&& split[0] == split[1] && split[1] == split[3]
                    this.type = 5;
                } else if (split.length == 4) {
                    this.type = 4;
                } else if (split.length == 5) {
                    this.type = 8;
                } else {
                    throw new RuntimeException("input error");
                }
            }

            boolean isDoubleWang() {
                return this.type == 1 || this.type == 2 || this.type == 3;
            }

            boolean isZd() {
                return this.type == 4 || this.isDoubleWang();
            }

            public Pai compare(Pai other) {
                if (this.isZd() && other.isZd()) {
                    if (this.isDoubleWang() && other.isDoubleWang()) {
                        // 对王怎么比?
                        return this.eles[0].equals("JOKER") ? this : other;
                    } else if (this.isDoubleWang()) {
                        return this;
                    } else if (other.isDoubleWang()) {
                        return other;
                    }

                    return this.eles[0].compareTo(other.eles[0]) > 0 ? this : other;
                } else if (this.isZd()) {
                    return this;
                } else if (other.isZd()) {
                    return other;
                }
                // 非炸弹只能同类型比较
                // !!! 3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER 顺序比
                else if (this.type == other.type) {
                    return map.get(this.eles[0]).compareTo(map.get(other.eles[0])) > 0 ? this : other;
                }

                return null; // ERROR
            }

            String join() {
                String ret = "";
                for (String str : eles) {
                    if (ret.length() > 0) {
                        ret += " ";
                    }
                    ret += str;
                }
                return ret;
            }

        }
    }

}
